<?php

namespace app\controller;

use think\facade\Request;
use app\BaseController;
use think\facade\Db;
use think\Response;
use think\response\Json;


class User extends BaseController
{
    public function index()
    {
        return 'hello';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function test()
    {
        return 'test';
    }
    public function check()
    {
        $data = Request::post();
        $input_userid = $data['userid'];
        $input_userpassword = $data['userpassword'];

        // 查询数据库中的用户密码
        $db_user = Db::table('userdata')->where('id', $input_userid)->find();

        if ($db_user) {
            // 检查用户密码是否匹配
            if ($input_userpassword === $db_user['userpassword']) {
                return json(['status' => true, 'message' => '登陆成功']);
            } else {
                return json(['status' => false, 'message' => '密码不正确']);
            }
        } else {
            // 用户名不存在
            return json(['status' => false, 'message' => '用户未找到']);
        }
    }
    public function signup()
    {
        $data = Request::post();
        $input_username = $data['username'];
        $input_userpassword = $data['userpassword'];
        $input_sex = $data['sex'];
        $input_birthday = $data['birthday'];
        $input_work = $data['work'];
        $input_intro = $data['intro'];
        $input_id = $data['phone'];

        // 查询数据库中的用户密码
        $db_user = Db::table('userwaitdata')->where('id', $input_id)->find();
        if (!empty($input_birthday)) {
            $input_birthday = date('Y-m-d', strtotime($input_birthday));
        }
        if (!$db_user) {
            $data0 =
                [
                    'id' => $input_id,
                    'username' => $input_username, 'userpassword' =>  $input_userpassword,
                    'sex' =>  $input_sex, 'birthday' => $input_birthday,
                    'work' => $input_work, 'intro' => $input_intro,
                ];
            Db::name('userwaitdata')->insert($data0);
            return json(['status' => true, 'message' => '注册成功等待管理员审核']);
            // return $data;
        } else {
            // 用户名存在
            return json(['status' => false, 'message' => '用户已存在']);
        }
    }
    public function getAllUserInfor()
    {
        $data = Request::post();
        $input_userid = $data['username'];

        // 查询数据库中的用户密码
        $db_user = Db::table('userdata')->where('id', $input_userid)->find();

        if ($db_user) {
            // 检查用户密码是否匹配
            return json(['status' => true, 'message' => '查询成功', 'data' => $db_user]);
        } else {
            // 用户名不存在
            return json(['status' => false, 'message' => '用户未找到']);
        }
    }
}
